var ctx=window.daijia.ctx;

var offset=0;
var limit=20;
var pagingLimit=20;//显示分页时的条数
var currentTotalElements=0;//当前显示总条数


var pageLoading=true;//是否能滚动加载
var hasShowPages=false;//是否已显示分页
var firstLoadPages=true;//分页初始化时 不自动加载

//筛选条件
var search_param=null;

var thisButton;

$(document).ready(function(){
	
	initdata(false);
	
	$("#searchButton").click(function(){
		search_param=$("#param").val();
		offset=0;
		initdata(false);
	});

	$(document).on("keydown","body",function(){
		if(window.event.keyCode == 13){
			$("#searchButton").click();
		}
	});
	
	$(document).on("click",".auditthis",function(){
		$("#dialog_audit_id").val($(this).attr("data-id"));
		$("#dialog_audit_money").html("金额："+$(this).parent().parent().find(".money").html());
		// $("#dialog_audit_bank").html("银行名："+$(this).parent().parent().find(".banks").html());
		// $("#dialog_audit_cardNumber").html("银行卡号："+$(this).parent().parent().find(".cardNumbers").html());
		// $("#dialog_audit_masterName").html("持卡人姓名："+$(this).parent().parent().find(".masterNames").html());
    	$("#dialog_audit_success").attr("disabled",false);
    	$("#dialog_audit_turndown").attr("disabled",false);
    	$('#dialog_audit').modal();
    });
	$("#dialog_audit_close").click(function(){
		$('#dialog_audit').modal("hide");
	});
	
	$("#dialog_audit_success").click(function(){
		audit("success");
	});
	
	$("#dialog_audit_turndown").click(function(){
		audit("turndown");
	});
	
	
});

function audit(result){
	$("#dialog_audit_success").attr("disabled",true);
	$("#dialog_audit_turndown").attr("disabled",true);
	$.post(ctx+"/driverRebateenchashment/audit",{'id':$("#dialog_audit_id").val(),'result':result},function(data){
        if(data.success){
        	$.scojs_message("操作成功", $.scojs_message.TYPE_OK);
        	$('#dialog_audit').modal("hide");
			initdata(false);
        }
        else{
        	$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
        }
	},'json');
}

//开始导出


function initdata(isappend){
	
	$.ajax({
        url : ctx+'/driverRebateenchashment/listData',
        type:'GET',
        async:false,
        dataType : 'json',
        data : {
        	"pager.offset":offset,
        	"limit":limit,
        	"param":search_param
        },
        success : function(result){
        	var newcontent = '';
        	var totalElements=result.totalElements;
        	
        	if(totalElements>0){
        		$(result.content).each(function(index,data){
        			var statusColor="";
        			var mainButton="";
        			var selectButton="";
        			
        			if(data.status==0){
        				statusColor="<span class='btn btn-xindan btn-xs'>待审核</span>";
        				mainButton="<a type='button' class='btn btn-primary btn-xs auditthis' data-id='"+data.id+"'>审核</a>";
        			}
        			else if(data.status==1){
        				statusColor="<span class='btn btn-success btn-xs'>已同意</span>";
        				mainButton="<a type='button' class='btn btn-primary btn-xs' disabled='disabled'>已同意</a>";
        			}
        			else if(data.status==2){
        				statusColor="<span class='btn btn-default btn-xs'>已驳回</span>";
        				mainButton="<a type='button' class='btn btn-primary btn-xs' disabled='disabled'>已驳回</a>";
        			}
        				
    				
        			
        			var allButton=mainButton;
        			
	    newcontent+="<tr class='grid-data tr-hui' style='display:none;'>"+
						"<td>"+statusColor+"</td>"+
						"<td>"+data.employRealName+"</td>"+
						"<td>"+data.employUserName+"</td>"+
						"<td>"+data.employPhone+"</td>"+
						"<td>"+data.companyName+"</td>"+
						"<td class='money'>"+data.opRebateMoney+"</td>"+
						"<td>"+new Date(data.created).Format("yyyy-MM-dd hh:mm")+"</td>"+
						// "<td class='banks'>"+data.bank+"</td>"+
						// "<td class='cardNumbers'>"+data.cardNumber+"</td>"+
						// "<td class='masterNames'>"+data.masterName+"</td>"+
						"<td>"+allButton+"</td>"+
					"</tr>";
        		});
        		
        	}
        	if(!isappend){
        		$(".table tbody .grid-data").remove();
        	}
        	$(".table tbody").append(newcontent);
        	
        	$(".table tbody .grid-data").each(function(index,element){
        		$(element).fadeIn(200+(index*35));
        	});
        	
        	
        	if(!hasShowPages){
        		if(!isappend){
            		currentTotalElements=result.content.length;
            		pageLoading=true;
            	}
        		else{
            		if(currentTotalElements<totalElements){
            			currentTotalElements+=result.content.length;
            			pageLoading=true;
            		}
            		else{
            			pageLoading=false;
            		}
            	}
        	}

        	if(hasShowPages){
    			firstLoadPages=true;
        		$(".pageshow li a").html("共"+totalElements+" 条数据");
        		$(".pagedata").paging(totalElements, {
        			format: '[< ncnnn >]',
        			perpage: pagingLimit,
        			page:(offset/limit)+1,
        			onSelect: function (page) {
        				limit=pagingLimit;
        				offset=(page-1)*limit;
        				if(!firstLoadPages){
        					initdata(false);
        				}
        				firstLoadPages=false;
        		    	return false;
        			},
        			onFormat: function (type) {
        				switch (type) {
        				case 'block': // n and c
        					if (!this.active)
        						return '';
        					else if (this.value != this.page)
        						return '<li><a href="#">' + this.value + '</a></li>';
        					return '<li class="active"><a href="#">' + this.value + '</a></li>';
        				case 'next': // >
        					return '<li><a href="#">&rsaquo;</a><li>';
        				case 'prev': // <
        						return '<li><a href="#">&lsaquo;</a><li>';
        				case 'first': // [
        						return '<li><a href="#">&laquo;</a><li>';
        				case 'last': // ]
        						return '<li><a href="#">&raquo;</a></li>';
        				}
        			}
        		});
        		
        	}
        	else{
        		//滚动达到pagingLimit条开始显示分页
//        		if(currentTotalElements==pagingLimit){
            		
            		pageLoading=false;
            		hasShowPages=true;
            		
            		$(".pageshow").show();
            		$(".pagedata").show();
            		$(".pageshow li a").html("共"+totalElements+" 条数据");
            		$(".pagedata").paging(totalElements, {
            			format: '[< ncnnn >]',
            			perpage: pagingLimit,
            			onSelect: function (page) {
            				limit=pagingLimit;
            				offset=(page-1)*limit;
            				if(!firstLoadPages){
            					initdata(false);
            				}
            				firstLoadPages=false;
            		    	return false;
            			},
            			onFormat: function (type) {
            				switch (type) {
            				case 'block': // n and c
            					if (!this.active)
            						return '';
            					else if (this.value != this.page)
            						return '<li><a href="#">' + this.value + '</a></li>';
            					return '<li class="active"><a href="#">' + this.value + '</a></li>';
            				case 'next': // >
            					return '<li><a href="#">&rsaquo;</a><li>';
            				case 'prev': // <
            						return '<li><a href="#">&lsaquo;</a><li>';
            				case 'first': // [
            						return '<li><a href="#">&laquo;</a><li>';
            				case 'last': // ]
            						return '<li><a href="#">&raquo;</a></li>';
            				}
            			}
            		});
            		
//            	}
        	}
        	
        }
	});
	
}

function showLoading(){
	if($('#loadblacklayer').length==0){
		var dw = $(".table").width();
		var dh = $(".table").height();
		var backlayer ="<div id='loadblacklayer'><div style='margin-top:"+(dh/2)+"px;margin-left:"+(dw/2)+"px;'><img src='"+ctx+"/assets/css/images/loading.gif' /></div></div>"
		$(".table tbody").append(backlayer);
		$('#loadblacklayer').css({"z-index":"9","background":"#000","opacity":"0.5","position":'absolute',"top":0,"width":dw,"height":dh});
	}
}

function hideLoading(){
	if($('#loadblacklayer').length>0){
		$('#loadblacklayer').remove();
	}
}
Date.prototype.Format = function(fmt) 
{ 
  var o = { 
    "M+" : this.getMonth()+1,                 //月份 
    "d+" : this.getDate(),                    //日 
    "h+" : this.getHours(),                   //小时 
    "m+" : this.getMinutes(),                 //分 
    "s+" : this.getSeconds(),                 //秒 
    "q+" : Math.floor((this.getMonth()+3)/3), //季度 
    "S"  : this.getMilliseconds()             //毫秒 
  }; 
  if(/(y+)/.test(fmt)) 
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
  for(var k in o) 
    if(new RegExp("("+ k +")").test(fmt)) 
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
  return fmt; 
}
